package scanner.com.activity;

import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.view.View;
import com.haha.dialog.MakeDialog;

import scanner.com.entity.ContactEntity;
import scanner.com.scanner.R;

/**
 * Created by longteng on 2016-6-7.
 */
public class MakeActivity extends BaseActivity {

    private View makeContactBtn,makeUrlBtn,makeTextBtn;
    private Dialog makeContactDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make);
        findView();
        initView();
    }

    @Override
    protected void findView() {
        makeContactBtn = findViewById(R.id.make_contact);
        makeUrlBtn = findViewById(R.id.make_url);
        makeTextBtn = findViewById(R.id.make_text);
    }

    @Override
    protected void initView() {
        makeContactBtn.setOnClickListener(onClickListener);
        makeUrlBtn.setOnClickListener(onClickListener);
        makeTextBtn.setOnClickListener(onClickListener);
        initDialog();
    }

    private void initDialog(){
        makeContactDialog = new MakeDialog(MakeActivity.this,R.layout.make_contact_dialog);
        makeContactDialog.findViewById(R.id.choose_contact_btn).setOnClickListener(onClickListener);
        makeContactDialog.findViewById(R.id.new_contact_btn).setOnClickListener(onClickListener);
        makeContactDialog.findViewById(R.id.close_btn).setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.make_contact:
                    makeContactDialog.show();
                    break;
                case R.id.make_url:
                    startActivity(new Intent(MakeActivity.this,UrlActivity.class));
                    break;
                case R.id.make_text:
                    startActivity(new Intent(MakeActivity.this,Tsontd.class));
                    break;
                case R.id.choose_contact_btn:
                    jsContactActivity();
                    break;
                case R.id.new_contact_btn:

                    break;
                case R.id.close_btn:
                    makeContactDialog.dismiss();
                    break;
            }
        }
    };

    private void jsContactActivity(){
        Intent intent =  new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent,2);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode != 2 || resultCode != RESULT_OK) return;
        Uri uri = data.getData();
        ContactEntity contactEntity = new ContactEntity();
        Cursor cursor = getContentResolver().query(uri,null,null,null,null);
        if(cursor == null) return;
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        cursor.close();
        contactEntity.setName(name);
        Cursor phoneCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,null,null);
        while(phoneCur != null && phoneCur.moveToNext()){
            String phone = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactEntity.getPhoneNums().add(phone);
        }
        phoneCur.close();
        Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null,null);
        while(emailCur != null && emailCur.moveToNext()){
            String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            contactEntity.getEmails().add(email);
        }
        emailCur.close();
        String whereStr = ContactsContract.Data.CONTACT_ID + " = " + contactId + " AND " + ContactsContract.Data.MIMETYPE
                + " = " + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE;
        Cursor addressCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI,null,whereStr,null,null);
        if (addressCur != null && addressCur.moveToFirst()){
            contactEntity.setAddress(addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
        }
        addressCur.close();


        Intent intent = new Intent(MakeActivity.this,EncodeContact.class);
        intent.putExtra(EncodeContact.KEY_CONTACT_ENTITY,contactEntity);
        startActivity(intent);
    }


}

